//login.js
import WxValidate from '../../utils/WxValidate.js'

const app = getApp();
Page({
  data: {
    mobileNum: null,
    verifyCode: null,
    warnInfo: null,
    displayWarn: "display:none",
    time: '获取验证码',
    currentTime: 61,
    disable: false,
    navbarData: {
      showCapsule: false, //是否显示左上角图标   1表示显示    0表示不显示
      title: '登录', //导航栏 中间的标题
    },
    navHeight: app.globalData.statusBarHeight * 2 + 86,
  },
  initValidate() {
    const rules = {

      mobileNum: {
        required: true,
        tel: true,
      },
      verifyCode: {
        required: true,
        minlength: 4
      },

    }
    // 验证字段的提示信息，若不传则调用默认的信息
    const messages = {
      mobileNum: {
        required: '请输入11位手机号码',
        tel: '请输入正确的手机号码',
      },
      verifyCode: {
        required: '请输入验证码',
        minlength: '请输入正确的验证码'
      },
    }
    // 创建实例对象
    this.WxValidate = new WxValidate(rules, messages)
  },
  onLoad: function () {
    this.initValidate();
  },
  inputMobile: function (e) {
    this.setData({
      mobileNum: e.detail.value
    });
  },
  getCode: function () {
    var that = this;
    var currentTime = that.data.currentTime
    // var inerval = wx.setinterval(function () { }, durtion)
    var interval = setInterval(function () {
      currentTime--;
      that.setData({
        time: currentTime + '秒可重新获取',
        disable: true
      })
      if (currentTime <= 0) {
        clearInterval(interval);
        that.setData({
          time: '获取验证码',
          currentTime: 61,
          disable: false
        })
      }
    }, 1000)
  },
  getVerifyCode: function (e) {
    this.WxValidate.checkForm({ mobileNum: this.data.mobileNum });
    const error = this.WxValidate.errorList[0];
    if (error.param == "mobileNum") {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none',
        duration: 1000
      })
    } else {
      this.getCode();
      wx.request({
        url: app.getUrl('/xcx/smscode'),
        method: 'GET',
        header: wx.getStorageSync('cookie'),
        data: {
          mobile: this.data.mobileNum
        },
        success(res) {
          if (res.data.code == 0) {
          } else {
            wx.showToast({
              title: res.data.message,
              icon: 'none'
            })
          }
        }
      })
    }
  },
  toSubmit: function (e) {
    //手机号验证码验证
    const params = e.detail.value
    if (!this.WxValidate.checkForm(params)) {
    
      const error = this.WxValidate.errorList[0]
      wx.showToast({
        title: error.msg,
        icon: 'none',
        duration: 1000
      })
      // this.showWarnInfo(error)
    } else {
      wx.request({
        url: app.getUrl('/xcx/mobileVerify'),
        method: 'GET',
        header: wx.getStorageSync('cookie'),
        data: {
          mobile: e.detail.value.mobileNum,
          verifyCode: e.detail.value.verifyCode
        },
        success(res) {//成功进行结果的返回
          app.globalData.userMobile = e.detail.value.mobileNum
           //手机号和验证码都正确的情况下进入
          if (res.data.code == 0) {
            if(res.data.data){
              app.globalData.account = res.data.data;
            }
            wx.navigateTo({
              url: '../guide/queue/queue',
            })
          } else if (res.data.code == 200) {
            app.globalData.userList = res.data.data;
            wx.navigateTo({
              url: '../guide/user_list/userList',
            })
          } else if (res.data.code == 201){
            app.globalData.userList = res.data.data;
            wx.navigateTo({
              url: '../guide/queue_list/queueList?ptUserId=' + res.data.data[0].id,
            })
          } else {
            wx.showToast({
              title: '验证码有误',
              icon: 'none'
            })
          }
        }
      })
    }
  },
  // showWarnInfo(error) {
  //   // 当前page是this对象
  //   let page = this;
  //   // 延时时间等待
  //   let delayTime = 1;
  //   // 延时等待毫秒,现设置为1000
  //   let delayMillsecond = 1000;
  //   // 调用显示警告函数
  //   showWran(page, error, delayTime, delayMillsecond);
  // },
})

// function showWran(page, error, delayTime, delayMillsecond) {
//   let timesRun = 0;
//   let interval = setInterval(function () {
//     timesRun += delayTime;
//     if (timesRun === delayTime) {
//       clearInterval(interval);
//     }
//     page.setData({
//       warnInfo: error.msg,
//       displayWarn: 'display:none'
//     });
//   }, delayMillsecond);
//   page.setData({
//     warnInfo: error.msg,
//     displayWarn: 'display:block'
//   });
// }